/*
 * @Author: your name
 * @Date: 2021-07-13 07:17:12
 * @LastEditTime: 2021-07-13 08:45:02
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /egl/src/main.c
 */
/**
 * @file main.c
 * @brief framebuffer测试工程
 * @author wangkun (wangkun@csmicro.com)
 * @version 1.0
 * @date 2021-07-03
 * 
 * @copyright Copyright (c) 2021  武汉凌久微电子有限公司
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version <th>Author  <th>Description
 * <tr><td>2021-07-03 <td>1.0     <td>wangh     <td>内容
 * </table>
 */
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <linux/fb.h>
#include <linux/ioctl.h>
#include <sys/mman.h>

#include <EGL/egl.h>

int main(int argc, char **argv)
{   
    EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
    if (display == EGL_NO_DISPLAY) {
        EGLint error = eglGetError();   
        printf("error: %d\n", error);
    } else {
        printf("create ok\n");
    }

    EGLint majorVersion;
    EGLint minorVersion;
    if (!eglInitialize(display, &majorVersion, &minorVersion)) {
        EGLint error = eglGetError();   
        printf("error: %d\n", error);
    } else {
        printf("major: %d, minor: %d\n", majorVersion, minorVersion);
    }

    EGLint attribList[] = {
        //EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT,
        EGL_RED_SIZE, 8,
        EGL_GREEN_SIZE, 8,
        EGL_BLUE_SIZE, 8,
        EGL_ALPHA_SIZE, 8,
        EGL_NONE
    };


    const EGLint MaxConfigs = 10;
    EGLConfig configs[MaxConfigs]; // We'll only accept 10 configs
    EGLint numConfigs;
    if (!eglChooseConfig(display, attribList, configs, MaxConfigs, &numConfigs)) {
    // Something didn't work … handle error situation
        printf("chooseConfig error");
        return -1;
    } 

    // Everything's okay. Continue to create a rendering surface
    printf("chooseConfig ok, config counts: %d\n", numConfigs);

    const EGLint attribList2[] = {
        EGL_CONTEXT_CLIENT_VERSION, 3,
        EGL_NONE
    };

    EGLContext context = eglCreateContext(display, configs[0], EGL_NO_CONTEXT, attribList2);

    if (context == EGL_NO_CONTEXT) {
        EGLint error = eglGetError();
        if (error == EGL_BAD_CONFIG) {
            printf("Handle error and recover\n");
        }
    }

    printf("done ok\n");

    
    EGLint attribList1[] = {
        EGL_RENDER_BUFFER, EGL_BACK_BUFFER,
        EGL_NONE
    };

    // 这里先省略了创建原生窗口的过程
    EGLSurface window = eglCreateWindowSurface(display, configs[0], EGL_NO_SURFACE, attribList1);

    if (window == EGL_NO_SURFACE) {
        switch (eglGetError()) {
        case EGL_BAD_MATCH:
            // Check window and EGLConfig attributes to determine
            // compatibility, or verify that the EGLConfig
            // supports rendering to a window,
            break;
        case EGL_BAD_CONFIG:
            // Verify that provided EGLConfig is valid
            break;
        case EGL_BAD_NATIVE_WINDOW:
            // Verify that provided EGLNativeWindow is valid
            break;
        case EGL_BAD_ALLOC:
            // Not enough resources available. Handle and recover
            break;
        }
    }

   
    EGLBoolean bRes = eglMakeCurrent(display, // 指定显示的连接
                          window, // EGL 绘图表面
                          window, // EGL 读取表面
                          context); // 指定连接到该表面的上下文

    eglSwapBuffers();
    eglDestroySurface();
    eglDestroyContext();
    eglTerminate(display);

}